package com.blank.shop.hystrix.consumer;

import com.blank.shop.hystrix.ShopHystrixApplication;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <br/>Date 2021/8/31
 * <br/>Time 14:28:02
 *
 * @author _blank
 */
@DefaultProperties(defaultFallback = "hystrixGlobalFallbackHandler")
@Profile({ShopHystrixApplication.SERVER_CONSUMER})
@RestController
public class HystrixConsumerController {

    @Resource
    private HystrixConsumerService hystrixConsumerService;

    /**
     * HystrixCommand 加了@DefaultProperties属性注解，并且没有写具体方法名字，就用统一全局的
     */
    @HystrixCommand
    @GetMapping(path = {"simulation-normal"})
    public ResponseEntity<String> simulationNormal() {
        return this.hystrixConsumerService.simulationNormal();
    }

    @HystrixCommand(
            fallbackMethod = "simulationTimeOutFallbackHandler",
            commandProperties = {@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "1500")}
    )
    @GetMapping(path = {"simulation-time-out"})
    public ResponseEntity<String> simulationTimeOut() {
        return this.hystrixConsumerService.simulationTimeOut();
    }

    public ResponseEntity<String> simulationTimeOutFallbackHandler() {
        return ResponseEntity.ok("服务提供方出错或超时，当前消费服务可能出错！，执行兜底方案，请检查服务！");
    }

    /**
     * hystrix全局方案
     *
     * @return org.springframework.http.ResponseEntity
     */
    public ResponseEntity<String> hystrixGlobalFallbackHandler() {
        return ResponseEntity.ok("hystrix全局兜底方案！");
    }

}
